package aad.storyboard.model;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

public class Particle extends GraphicalObject{
	
	private ParticleState stateAlive;
	private ParticleState stateDead;
	private ParticleState stateCurrent;	
	
	public static final int DEFAULT_LIFETIME 	= 100;	
	public static final int MAX_DIMENSION		= 5;	// the maximum width or height
	public static final int MAX_SPEED			= 10;	// maximum speed (per update)	
	
	private float width;		// width of the particle
	private float height;		// height of the particle	
	private double xv, yv;		// vertical and horizontal velocity
	private int age;			// current age of the particle
	private int lifetime;		// particle dies when it reaches this value
	private int color;			// the color of the particle
	private Paint paint;		// internal use to avoid instantiation	
	
	
	public Particle(int x, int y, int color) {
		this.posX = x;
		this.posY = y;
		
		stateAlive=new ParticleAliveState(this);
		stateDead=new ParticleDeadState(this);
		stateCurrent=stateAlive;
				
		this.width = rndInt(1, MAX_DIMENSION);
		this.height = this.width;
		this.lifetime = DEFAULT_LIFETIME;
		this.age = 0;
		this.xv = (rndDbl(0, MAX_SPEED * 2) - MAX_SPEED);
		this.yv = (rndDbl(0, MAX_SPEED * 2) - MAX_SPEED);
		// smoothing out the diagonal speed
		if (xv * xv + yv * yv > MAX_SPEED * MAX_SPEED) {
			xv *= 0.7;
			yv *= 0.7;
		}
		//this.color = Color.argb(255, rndInt(0, 255), rndInt(0, 255), rndInt(0, 255));
		this.color=color;
		this.paint = new Paint(this.color);
	}
	
	
	public float getWidth() {
		return width;
	}

	public void setWidth(float width) {
		this.width = width;
	}

	public float getHeight() {
		return height;
	}

	public void setHeight(float height) {
		this.height = height;
	}	

	public double getXv() {
		return xv;
	}

	public void setXv(double xv) {
		this.xv = xv;
	}

	public double getYv() {
		return yv;
	}

	public void setYv(double yv) {
		this.yv = yv;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getLifetime() {
		return lifetime;
	}

	public void setLifetime(int lifetime) {
		this.lifetime = lifetime;
	}

	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}
	
	
	public boolean isAlive() {
		return stateCurrent.equals(stateAlive);
	}
	public boolean isDead() {
		return stateCurrent.equals(stateDead);
	}

	
	
	public void reset(float x, float y) {		
		stateCurrent.reset(x, y);
	}
	
	public void setNewState(ParticleState state)	{
		stateCurrent=state;
	}

	// Return an integer that ranges from min inclusive to max inclusive.
	static int rndInt(int min, int max) {
		return (int) (min + Math.random() * (max - min + 1));
	}

	static double rndDbl(double min, double max) {
		return min + (max - min) * Math.random();
	}
	
	public void update() {
		stateCurrent.update();
	}
	
	public void update(Rect container) {
		// update with collision
		stateCurrent.update(container);
		update();
	}

	public void draw(Canvas canvas) {

		paint.setColor(this.color);
		//canvas.drawRect(this.posX, this.posY, this.posX + this.width, this.posY + this.height, paint);
		canvas.drawCircle(posX, posY, width, paint);
	}

	public ParticleState getStateDead() {		
		return stateDead;
	}

	public ParticleState getAliveState() {
		
		return stateAlive;
	}

}
